package com.example.another.navigationviewfragmentdemo;

import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;

import com.example.another.navigationviewfragmentdemo.fragment.FirstFragment;

public class BottomNavigationViewFragmentActivity extends AppCompatActivity {
    private BottomNavigationView mBottomNavigationView;
    private Fragment mFragment1, mFragment2, mFragment3, mFragment4, mFragment5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom_navigation_view_fragment);

        initViews();
    }

    private void initViews() {
        mBottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation_view);
        mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                showFragments(item.getItemId());
                return true;
            }
        });
        showFragments(R.id.menu1);
    }

    private void showFragments(int position) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

        switch (position) {
            case R.id.menu1:
                if (mFragment1 == null) {
                    mFragment1 = new FirstFragment();
                    fragmentTransaction.add(R.id.fragment_container, mFragment1);
                } else {
                    fragmentTransaction.show(mFragment1);
                }
                break;
            case R.id.menu2:
                if (mFragment2 == null) {
                    mFragment2 = new FirstFragment();
                    fragmentTransaction.add(R.id.fragment_container, mFragment2);
                } else {
                    fragmentTransaction.show(mFragment2);
                }
                break;
            case R.id.menu3:
                if (mFragment3 == null) {
                    mFragment3 = new FirstFragment();
                    fragmentTransaction.add(R.id.fragment_container, mFragment3);
                } else {
                    fragmentTransaction.show(mFragment3);
                }
                break;
            case R.id.menu4:
                if (mFragment4 == null) {
                    mFragment4 = new FirstFragment();
                    fragmentTransaction.add(R.id.fragment_container, mFragment4);
                } else {
                    fragmentTransaction.show(mFragment4);
                }
                break;
            case R.id.menu5:
                if (mFragment5 == null) {
                    mFragment5 = new FirstFragment();
                    fragmentTransaction.add(R.id.fragment_container, mFragment5);
                } else {
                    fragmentTransaction.show(mFragment5);
                }
                break;
            default:
                break;
        }
        fragmentTransaction.commit();

    }
}
